package com.ruoyi.plus.pub.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plus.constants.PlusDictConstants;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plus.pub.domain.PubOpinion;
import com.ruoyi.plus.pub.service.IPubOpinionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 意见反馈Controller
 *
 */
@RestController
@RequestMapping("/pub/opinion")
public class PubOpinionController extends BaseController {
    @Autowired
    private IPubOpinionService pubOpinionService;

    /**
     * 查询意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:list')")
    @GetMapping("/list")
    public TableDataInfo list(PubOpinion pubOpinion) {
        startPage();
        List<PubOpinion> list = pubOpinionService.selectPubOpinionList(pubOpinion);
        return getDataTable(list);
    }

    /**
     * 导出意见反馈列表
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:export')")
    @Log(title = "意见反馈导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PubOpinion pubOpinion) {
        List<PubOpinion> list = pubOpinionService.selectPubOpinionList(pubOpinion);
        ExcelUtil<PubOpinion> util = new ExcelUtil<>(PubOpinion.class);
        return util.exportExcel(list, "opinion");
    }

    /**
     * 获取意见反馈详细信息
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        PubOpinion pubOpinion = pubOpinionService.getById(id);
        if (pubOpinion != null && !PlusDictConstants.SYS_OPINION_READ.equals(pubOpinion.getStatus())) {
            //更改状态为已读
            pubOpinion.setStatus(PlusDictConstants.SYS_OPINION_READ);
            pubOpinionService.updateById(pubOpinion);
        }
        return AjaxResult.success(pubOpinion);
    }

    /**
     * 新增意见反馈
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:add')")
    @Log(title = "意见反馈添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PubOpinion pubOpinion) {
        return toAjax(pubOpinionService.save(pubOpinion));
    }

    /**
     * 修改意见反馈
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:edit')")
    @Log(title = "意见反馈修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PubOpinion pubOpinion) {
        return toAjax(pubOpinionService.updateById(pubOpinion));
    }

    /**
     * 删除意见反馈
     */
    @PreAuthorize("@ss.hasPermi('pub:opinion:remove')")
    @Log(title = "意见反馈删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pubOpinionService.removeByIds(Arrays.asList(ids)));
    }

    /* ---------------------------------------------------------------------------------------- */

}
